package com.pktechan.api.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: hby
 * Date: 14-11-26
 * Time: 上午9:58
 * To change this template use File | Settings | File Templates.
 */
public class Config {
    private static boolean IS_CONFIG_LOADED = false;//配置文件是否加载，已加载读取configMapper

    private static boolean IS_MAILTEXT_LOADED = false;
    private static Map<String, String> configMapper = new HashMap<String, String>();
    private static Map<String, String> readChineseMapper = new HashMap<String, String>();

    public synchronized static String readConfig(String key) {
        if (!IS_CONFIG_LOADED) {
            Properties properties = new Properties();
            InputStream is = null;
            try {
                is = Config.class.getResourceAsStream("/message.properties");
                properties.load(is);
                //  InputStream inputStream=new BufferedInputStream(new FileInputStream("/config.properties"));
                // properties.load(inputStream);
                Iterator<String> iterator = properties.stringPropertyNames().iterator();

                while (iterator.hasNext()) {
                    String k = iterator.next();
                    configMapper.put(k, properties.getProperty(k));
                }
                IS_CONFIG_LOADED = true;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("加载config.properties配置文件失败！");
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return configMapper.get(key);
    }

    public synchronized static String readChinese(String key) {
        if (!IS_MAILTEXT_LOADED) {
            Properties properties = new Properties();
            InputStreamReader is = null;
            try {
                is = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("/message.properties"), "utf-8");
                properties.load(is);

                Iterator<String> iterator = properties.stringPropertyNames().iterator();

                while (iterator.hasNext()) {
                    String k = iterator.next();
                    readChineseMapper.put(k, properties.getProperty(k));
                }
                IS_MAILTEXT_LOADED = true;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("加载秘钥配置文件失败！");
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return readChineseMapper.get(key);
    }

}
